import { Object3D, Raycaster, WebGLRenderTarget } from "three";

export const PickHelper: EditorPlugin = {
  install: (editor: Editor) => {
    const raycaster = new Raycaster();
    let pickedObject: Object3D | null = null;
    let pickedObjectSavedColor = 0;
    editor.emitter.on("render", () => {
      if (pickedObject) {
        //@ts-ignore
        pickedObject.material.color.set(pickedObjectSavedColor);
        pickedObject = null;
      }
      raycaster.setFromCamera(editor.pointer, editor.camera);
      const intersectedObjects = raycaster.intersectObjects(
        editor.scene.children
      );
      if (intersectedObjects.length) {
        pickedObject = intersectedObjects[0].object;
        //@ts-ignore
        pickedObjectSavedColor = pickedObject.material.color.getHex();
        //@ts-ignore
        pickedObject.material.color.set("#f00");
      }
    });
  },
};
